package service.exercise.medium.leetcode.editor.cn;

/**
 * @author yangcheng(darren)
 * @date 2021/07/25
 * @copyright 2021 Bizfocus All Rights Reserved
 * @description
 */
class Solution {
    private boolean[][] visited; // 是否访问过

    public boolean hasPath(int[][] maze, int[] start, int[] destination) {
        int m = maze.length;
        int n = maze[0].length;
        visited = new boolean[m][n];
        helper(maze, start);
        return visited[destination[0]][destination[1]];
    }

    public void helper(int[][] maze, int[] start) {
        int m = maze.length;
        int n = maze[0].length;
        if (visited[start[0]][start[1]]) return;
        visited[start[0]][start[1]] = true;
        int[] dx = {-1, 0, 0, 1};
        int[] dy = {0, -1, 1, 0};
        for (int i = 0; i < 4; i++) {
            int x = start[0] + dx[i];
            int y = start[1] + dy[i];
            while (x >= 0 && y >= 0 && x < m && y < n && maze[x][y] == 0) {
                x = x + dx[i];
                y = y + dy[i];
            }
            x -= dx[i];
            y -= dy[i];
            helper(maze, new int[]{x, y});
        }
    }
}
